package com.uetoken.cn.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.uetoken.cn.R;
import com.uetoken.cn.adapter.ShippingAddressAdapter;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.bean.ShippingAddressBean;
import com.uetoken.cn.common.ApiHelper;
import com.uetoken.cn.network.OkHttpUtils;
import com.uetoken.cn.network.Params;
import com.uetoken.cn.network.WebResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 收货地址
 */
public class ShippingAddressActivity extends BaseActivity implements WebResponse{

    @BindView(R.id.iv_activity_shipping_Address_back)
    ImageView    mIvBack;
    @BindView(R.id.tv_activity_shipping_Address_title)
    TextView     mTvTitle;
    @BindView(R.id.ry_activity_shipping_Address)
    RecyclerView mRyAddress;
    @BindView(R.id.tv_activity_shipping_Address_add)
    TextView     mTvAdd;
    @BindView(R.id.rl_activity_shipping_Address_no)
    RelativeLayout mRlNo;
    /**
     * 获取收货地址列表的网络请求码
     */
    private static final int NETWORK_REQUEST_GET_ADDRESS = 10;
    /**
     * 跳转到新增地址的请求码
     */
    private static final int INTENT_NEW_SHIPPING_ADDRESS_CODE = 20;
    private ShippingAddressAdapter             mAdapter;
    private List<ShippingAddressBean.DataBean> mDataBeanList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_harvest_address;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this,android.R.color.white),true);
        BarUtils.setStatusBarLightMode(this,true);

        mTvTitle.setText(getResources().getString(R.string.str_sp_address));
    }

    @Override
    public void initData() {
        mRyAddress.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mDataBeanList = new ArrayList<>();
        mAdapter = new ShippingAddressAdapter(R.layout.item_shipping_address, mDataBeanList);
        mRyAddress.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(ShippingAddressActivity.this,NewShippingAddressActivity.class);
                intent.putExtra("address","have");
                intent.putExtra("info",mDataBeanList.get(position));
                startActivityForResult(intent,INTENT_NEW_SHIPPING_ADDRESS_CODE);
            }
        });

        getAddressList();
    }

    @Override
    public void init() {

    }

    /**
     * 获取收货地址列表
     */
    private void getAddressList() {
        if (mDataBeanList.size()!=0){
            mDataBeanList.clear();
        }
        showLoading();
        OkHttpUtils.getInstance().getStringWithParam(this, ApiHelper.getAddressUrl(),NETWORK_REQUEST_GET_ADDRESS, Params.getAddressUrlParams());
    }


    @OnClick({R.id.iv_activity_shipping_Address_back,R.id.tv_activity_shipping_Address_add})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.iv_activity_shipping_Address_back:
                finish();
                break;
            case R.id.tv_activity_shipping_Address_add:
                Intent intent = new Intent(ShippingAddressActivity.this,NewShippingAddressActivity.class);
                intent.putExtra("address","no");
                startActivityForResult(intent,INTENT_NEW_SHIPPING_ADDRESS_CODE);
                break;
        }
    }

    @Override
    public void onSuccessResponse(Call call, String response, int requestCode) throws IOException {
        dissmissDialog();
        ShippingAddressBean shippingAddressBean = new Gson().fromJson(response,ShippingAddressBean.class);
        if (shippingAddressBean.getResult()>0){
            if (shippingAddressBean.getData().size()>0){
                mRlNo.setVisibility(View.GONE);

                mDataBeanList.addAll(shippingAddressBean.getData());
                mAdapter.notifyDataSetChanged();
            }else{
                mRlNo.setVisibility(View.VISIBLE);
            }
        }else{
            ToastUtils.showShort(shippingAddressBean.getMessage());
        }
    }

    @Override
    public void onFailResponse(Call call, IOException e, int requestCode) {
        dissmissDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==INTENT_NEW_SHIPPING_ADDRESS_CODE && resultCode==RESULT_OK){
            getAddressList();
        }
    }
}
